using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Windows.Media.Imaging;
using System.Xml.Linq;
namespace INTEX.DataBase.CE
{
    public partial class Mapper
    {
        private List<VKKOPF_EXT> MapVKKOPF_EXT(XElement item2, bool mergedb)
        {
            List<VKKOPF_EXT> result = new List<VKKOPF_EXT>();
            var item4 = from item3 in item2.Elements()
                        select new VKKOPF_EXT
                        {  
                            MAINKEY = (string)item3.Element("MAINKEY"),
                            VKKMANDANT = (string)item3.Element("VKKMANDANT"),
                            VKKAUFKRS = (string)item3.Element("VKKAUFKRS"),
                            VKKAUFLFD = (string)item3.Element("VKKAUFLFD"),
                            VKKNEUBEN = (string)item3.Element("VKKNEUBEN"),
                            VKKNEUDAT = (string)item3.Element("VKKNEUDAT"),
                            VKKKUNDNR = (string)item3.Element("VKKKUNDNR"),
                            KUNDNAME = (string)item3.Element("KUNDNAME"),
                            VKKVB = (string)item3.Element("VKKVB"),
                            VKKAENDZL = (string)item3.Element("VKKAENDZL"),
                            VKKSACHBE = (string)item3.Element("VKKSACHBE"),
                            Photo = (string)item3.Element("Photo")
                        }; 
                        
          result = item4.ToList();
          if (mergedb)
            {                  
               var rowsexist = from u in LocalDB.VKKOPF_EXT.ToList().AsQueryable()
                                           join uc in result.AsQueryable() on  u.MAINKEY equals  uc.MAINKEY
                                           select u;
                LocalDB.VKKOPF_EXT.DeleteAllOnSubmit(rowsexist);
                LocalDB.SubmitChanges();
                LocalDB.VKKOPF_EXT.InsertAllOnSubmit(result);
                LocalDB.SubmitChanges();
            }
        
            return result;
        }
    }
    [Table(Name = "VKKOPF_EXT")]
    public class VKKOPF_EXT : INotifyPropertyChanged,INotifyPropertyChanging
    {
        private string _MAINKEY="0";
        [Column(IsPrimaryKey = true, CanBeNull = false)]
        public string MAINKEY
        {
            get
            {
                return _MAINKEY;
            }
            set
            {
                if (value != _MAINKEY)
                {
                    NotifyPropertyChanging("MAINKEY");
                     _MAINKEY = value;
                     NotifyPropertyChanged("MAINKEY");
                }
            }
        }
        [Column(IsVersion = true)]
        private Binary _version;
   
        private string  _VKKMANDANT="0";
        [Column]
        public string VKKMANDANT
        {
            get
            {
                return _VKKMANDANT;
            }
            set
            {
                if (value != _VKKMANDANT)
                {
                    NotifyPropertyChanging("VKKMANDANT");
                     _VKKMANDANT = value;
                    NotifyPropertyChanged("VKKMANDANT");
                }
            }
        }
        private string  _VKKAUFKRS="0";
        [Column]
        public string VKKAUFKRS
        {
            get
            {
                return _VKKAUFKRS;
            }
            set
            {
                if (value != _VKKAUFKRS)
                {
                    NotifyPropertyChanging("VKKAUFKRS");
                     _VKKAUFKRS = value;
                    NotifyPropertyChanged("VKKAUFKRS");
                }
            }
        }
        private string  _VKKAUFLFD="0";
        [Column]
        public string VKKAUFLFD
        {
            get
            {
                return _VKKAUFLFD;
            }
            set
            {
                if (value != _VKKAUFLFD)
                {
                    NotifyPropertyChanging("VKKAUFLFD");
                     _VKKAUFLFD = value;
                    NotifyPropertyChanged("VKKAUFLFD");
                }
            }
        }
        private string  _VKKNEUBEN="0";
        [Column]
        public string VKKNEUBEN
        {
            get
            {
                return _VKKNEUBEN;
            }
            set
            {
                if (value != _VKKNEUBEN)
                {
                    NotifyPropertyChanging("VKKNEUBEN");
                     _VKKNEUBEN = value;
                    NotifyPropertyChanged("VKKNEUBEN");
                }
            }
        }
        private string  _VKKNEUDAT="0";
        [Column]
        public string VKKNEUDAT
        {
            get
            {
                return _VKKNEUDAT;
            }
            set
            {
                if (value != _VKKNEUDAT)
                {
                    NotifyPropertyChanging("VKKNEUDAT");
                     _VKKNEUDAT = value;
                    NotifyPropertyChanged("VKKNEUDAT");
                }
            }
        }
        private string  _VKKKUNDNR="0";
        [Column]
        public string VKKKUNDNR
        {
            get
            {
                return _VKKKUNDNR;
            }
            set
            {
                if (value != _VKKKUNDNR)
                {
                    NotifyPropertyChanging("VKKKUNDNR");
                     _VKKKUNDNR = value;
                    NotifyPropertyChanged("VKKKUNDNR");
                }
            }
        }
        private string  _KUNDNAME="0";
        [Column]
        public string KUNDNAME
        {
            get
            {
                return _KUNDNAME;
            }
            set
            {
                if (value != _KUNDNAME)
                {
                    NotifyPropertyChanging("KUNDNAME");
                     _KUNDNAME = value;
                    NotifyPropertyChanged("KUNDNAME");
                }
            }
        }
        private string  _VKKVB="0";
        [Column]
        public string VKKVB
        {
            get
            {
                return _VKKVB;
            }
            set
            {
                if (value != _VKKVB)
                {
                    NotifyPropertyChanging("VKKVB");
                     _VKKVB = value;
                    NotifyPropertyChanged("VKKVB");
                }
            }
        }
        private string  _VKKAENDZL="0";
        [Column]
        public string VKKAENDZL
        {
            get
            {
                return _VKKAENDZL;
            }
            set
            {
                if (value != _VKKAENDZL)
                {
                    NotifyPropertyChanging("VKKAENDZL");
                     _VKKAENDZL = value;
                    NotifyPropertyChanged("VKKAENDZL");
                }
            }
        }
        private string  _VKKSACHBE="0";
        [Column]
        public string VKKSACHBE
        {
            get
            {
                return _VKKSACHBE;
            }
            set
            {
                if (value != _VKKSACHBE)
                {
                    NotifyPropertyChanging("VKKSACHBE");
                     _VKKSACHBE = value;
                    NotifyPropertyChanged("VKKSACHBE");
                }
            }
        }
      public void ConvertToFile(IsolatedStorageFile ISO,ObservableCollection<VKKOPF_EXT> ListInput, string filep = "", string setguid="")
        {           
            try
            {
                string localfile = filep;
                if (filep.Trim() == "")
                {
                    if (setguid.Trim() == "")
                        setguid = Guid.NewGuid().ToString();
                    localfile = "Data\\PhoneDB\\VKKOPF_EXT\\VKKOPF_EXT" +setguid + ".xml";
                }
                string folder = "Data\\PhoneDB\\VKKOPF_EXT" ;
                if (!ISO.DirectoryExists(folder))
                {
                    ISO.CreateDirectory(folder);
                }
                ISO.DeleteFile(localfile);
                StreamWriter sw = new StreamWriter(ISO.CreateFile(localfile));
                XDocument xdoc = new XDocument();
                //Root Element
                XElement root = new XElement("VKKOPF_EXT");
                if (setguid.Trim() != "")
                {
                    root = new XElement("ITX"+setguid);
                }
                //Add root to file
                xdoc.Add(root);
                foreach (VKKOPF_EXT row in ListInput)
                {
                    XElement f = new XElement("VKKOPF_EXT");
                    XElement MAINKEY = new XElement("MAINKEY");
                     f.Add(MAINKEY);
                   
                        XElement VKKMANDANT = new XElement("VKKMANDANT");
                         VKKMANDANT.Value = row.VKKMANDANT;
                          f.Add(VKKMANDANT);
                        XElement VKKAUFKRS = new XElement("VKKAUFKRS");
                         VKKAUFKRS.Value = row.VKKAUFKRS;
                          f.Add(VKKAUFKRS);
                        XElement VKKAUFLFD = new XElement("VKKAUFLFD");
                         VKKAUFLFD.Value = row.VKKAUFLFD;
                          f.Add(VKKAUFLFD);
                        XElement VKKNEUBEN = new XElement("VKKNEUBEN");
                         VKKNEUBEN.Value = row.VKKNEUBEN;
                          f.Add(VKKNEUBEN);
                        XElement VKKNEUDAT = new XElement("VKKNEUDAT");
                         VKKNEUDAT.Value = row.VKKNEUDAT;
                          f.Add(VKKNEUDAT);
                        XElement VKKKUNDNR = new XElement("VKKKUNDNR");
                         VKKKUNDNR.Value = row.VKKKUNDNR;
                          f.Add(VKKKUNDNR);
                        XElement KUNDNAME = new XElement("KUNDNAME");
                         KUNDNAME.Value = row.KUNDNAME;
                          f.Add(KUNDNAME);
                        XElement VKKVB = new XElement("VKKVB");
                         VKKVB.Value = row.VKKVB;
                          f.Add(VKKVB);
                        XElement VKKAENDZL = new XElement("VKKAENDZL");
                         VKKAENDZL.Value = row.VKKAENDZL;
                          f.Add(VKKAENDZL);
                        XElement VKKSACHBE = new XElement("VKKSACHBE");
                         VKKSACHBE.Value = row.VKKSACHBE;
                          f.Add(VKKSACHBE);
                  root.Add(f);
                }
                sw.Write(xdoc);
                sw.Close();
                sw.Dispose();
            }
            catch (Exception ex)
            { 
            }
      
        }
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #region INotifyPropertyChanging Members

        public event PropertyChangingEventHandler PropertyChanging;

        // Used to notify that a property is about to change
        private void NotifyPropertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        #endregion
        public bool IsFirst=false;
        private string _Photo = "YES";
        public string Photo
        {
            get
            {
                return _Photo;
            }
            set
            {
                if (value != _Photo)
                {
                    NotifyPropertyChanging("Photo");
                    _Photo = value;
                    NotifyPropertyChanged("Photo");
                    NotifyPropertyChanged("PhotoISOSource");
                }
            }
        }
        public string PhotoSource
        {
            get
            {
                string image = string.Format("/Data/Images/{0}", "YES");
                if (!Photo.Contains("http://"))
                {
                    return string.Format("/Data/Images/{0}", Photo);
                }
                return Photo;
            }
        }
    public BitmapImage PhotoISOSource
        {
            get
            {
                string image = string.Format("Data/PhoneDB/Pictures/{0}.jpg",Photo);
                BitmapImage bi = new BitmapImage();
                IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();
                if(!iso.FileExists(image))
                    image = string.Format("Data/PhoneDB/Pictures/{0}.jpg", "YES");
                using (IsolatedStorageFileStream fileStream = iso.OpenFile(image, FileMode.Open, FileAccess.Read))
                {
                    //bi.SetSource(fileStream);
                }              
                return bi;
            }
        }                                        
    }
}
